package xin.mashibo.artcobutest.utils;

import android.util.Log;

import java.io.UnsupportedEncodingException;

/**
 * @author MaShibo
 *         on 2018/3/26
 */
public class ConvertUtils {

    private static final String TAG = "ConvertUtils";
    public static final String CHARSET_NAME = "GBK";

    private static byte[] hexStringToBytes(String hex) {
        byte[] bytes = new byte[hex.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) (Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16) & 0xFF);
        }
        return bytes;
    }

    public static String hexStringToText(String hex) {
        if (hex != null && !"".equals(hex.trim())) {
            try {
                return new String(hexStringToBytes(hex), CHARSET_NAME);
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "hexStringToText: " + e.getMessage(), e);
            }
        }
        return "";
    }

}
